package com.lmb.todo.service.impl;

import com.lmb.todo.dao.TodoDao;
import com.lmb.todo.pojo.Todo;
import com.lmb.todo.service.GroupService;
import com.lmb.todo.service.TodoService;
import com.lmb.todo.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmb
 */
@Service
public class TodoServiceImpl implements TodoService {

    @Autowired
    private TodoDao todoDao;

    @Autowired
    private GroupService groupService;

    @Override
    public List<Todo> getByGroupId(Integer groupId) {
        if (groupId > 0) {
            return todoDao.getByGroupId(groupId);
        }
        return null;
    }

    @Override
    public Integer updateDone(Integer id, Integer done) {
        if (id > 0) {
            return todoDao.updateDone(id, done, DateUtil.getCurrentDateStr());
        }
        return 0;
    }

    @Override
    public Integer updateStar(Integer id, Integer star) {
        if (id > 0) {
            return todoDao.updateStar(id, star, DateUtil.getCurrentDateStr());
        }
        return 0;
    }

    @Override
    public Integer update(Todo todo) {
        if (todo != null) {
            todo.setUpdateTime(DateUtil.getCurrentDateStr());
            return todoDao.update(todo);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateGroupId(Integer todoId, Integer groupId) {
        if (todoId > 0 && groupId > 0) {
            // 当前所在分组的groupId
            Integer beforeGroupId = todoDao.getGroupIdById(todoId);
            groupService.count(beforeGroupId, 1, "minus");
            groupService.count(groupId, 1, "add");

            return todoDao.updateGroupId(todoId, groupId);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteTodo(Integer id) {
        if (id > 0) {
            groupService.count(getGroupIdById(id), 1, "minus");
            return todoDao.deleteTodo(id);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer add(Todo todo, boolean useDefault) {
        if (todo != null) {
            todo.setCreateTime(DateUtil.getCurrentDateStr());
            if (useDefault) {
                todo.setDone(0);
                todo.setStar(0);
            }
            groupService.count(todo.getGroupId(), 1, "add");
            return todoDao.add(todo);
        }
        return 0;
    }

    @Override
    public List<Todo> getTodoByContent(String content, List<Integer> groupIdList) {
        if ("".equals(content)) {
            return new ArrayList<>();
        }
        return todoDao.getTodoByContent(content, groupIdList);
    }

    @Override
    public Integer getGroupIdById(Integer id) {
        if (id > 0) {
            return todoDao.getGroupIdById(id);
        }
        return null;
    }

}
